package controller

import (
	. "api/model"
	. "api/util"
	. "api/service"
	"net/http"
	"strconv"
	"github.com/gin-gonic/gin"
)

func GetPromotionOfficer(c *gin.Context) {
	s := PromotionOfficer{}

	ret, err := s.GetPromotionOfficer()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"status": 200, "msg": "success", "data": ret})
}

func CreatePromotionOfficer(c *gin.Context) {
	p := PromotionOfficer{}

	err := c.BindJSON(&p)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
	}

	pwd, err := p.CreatePromotionOfficer(p.Phone)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	err = SendMessage(p.Phone, pwd, "1606146")
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"status": 200, "msg": "success"})
}

func DeletePromotionOfficer(c *gin.Context) {
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	status, err := strconv.Atoi(c.Query("status"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	p := PromotionOfficer{}

	err = p.DeletePromotionOfficer(status, id)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"status": 200, "msg": "success"})
}

func GetPromotionOfficerInfo(c *gin.Context) {
	phone := c.Query("phone")
	token := c.Request.Header.Get("Token")

	claims, err := ParseCliamsToken(token)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}
	cid := int(claims.Id)

	p := PromotionOfficerInfo{}
	ret, err := p.GetPromotionOfficerInfo(cid, phone)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"status": 200, "msg": "success", "data": ret})
}

func GetApprentices(c *gin.Context) {
	phone := c.Query("phone")

	p := PromotionOfficer{}
	ret, num, err := p.GetApprentices(phone)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}
	if ret == nil {
		ret = make([]*Apprentice, 0)
	}

	c.JSON(http.StatusOK, gin.H{"status": 200, "msg": "success", "data": ret, "num": num})
}

func GetPromotionApprentices(c *gin.Context) {
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	p := PromotionOfficer{}
	ret, err := p.GetPromotionApprentices(id)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"status": 200, "msg": "success", "data": ret})
}

func DeletePromotionApprentices(c *gin.Context) {
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	aid, err := strconv.Atoi(c.Query("apprentice_id"))
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	p := PromotionOfficer{}
	err = p.DeletePromotionApprentices(id, aid)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"status": 200, "msg": "success"})
}